package cn.klaw.selenium.autoconfiguration;

import cn.klaw.selenium.SeleniumHelper;
import cn.klaw.selenium.consts.SeleniumConsts;
import cn.klaw.selenium.driver.ChromeBrowser;
import cn.klaw.selenium.properties.SeleniumProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.*;

@Configuration(proxyBeanMethods = false)
@Import(SeleniumHelper.class)
@Slf4j
public class SeleniumAutoConfiguration {

    @Bean
    @ConfigurationProperties(SeleniumConsts.SELENIUM)
    public SeleniumProperties getSeleniumProperties() {
        return new SeleniumProperties();
    }

    @Lazy
    @ConditionalOnMissingBean(name = SeleniumConsts.BROWSER_BEAN_PREFIX + SeleniumConsts.BROWSER_TYPE_CHROME)
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean(value = SeleniumConsts.BROWSER_BEAN_PREFIX + SeleniumConsts.BROWSER_TYPE_CHROME, initMethod = "init", destroyMethod = "close")
    public ChromeBrowser getChromeBrowser() {
        if(log.isDebugEnabled()){
            log.debug(SeleniumConsts.MARKER, "Init New Chrome");
        }
        return new ChromeBrowser();
    }
}
